/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uniscol.admin;

import fr.uniscol.UniscolMainController;
import fr.uniscol.model.EtablissementModel;
import fr.uniscol.service.IEtablissementService;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Christophe
 */
@Controller
public class AdminEtablissementController extends UniscolMainController {
    
    @Autowired
    private IEtablissementService etablissementService;

    @ModelAttribute("etablissements")
    public List<EtablissementModel> getAllEtablissements(){
        return etablissementService.getAllEtablissements();
    }
    
    @RequestMapping("/admin/etablissements")
    public String list(HttpSession session){
        return "administration/etablissements";
    }
    
    @RequestMapping("/admin/etablissement.create")
    public ModelAndView create(HttpSession session){
        
        EtablissementModel etablissement = new EtablissementModel();
        
        return new ModelAndView("administration/etablissement", "etablissement", etablissement);
    }
    
    @RequestMapping("/admin/etablissement.edit")
    public ModelAndView edit(@RequestParam("etablissementid") long etabId, HttpSession session){
        
        EtablissementModel etablissement = etablissementService.getEtablissementById(etabId);
        
        return new ModelAndView("administration/etablissement", "etablissement", etablissement);
    }
    
    @RequestMapping(value = "/admin/etablissement.save", method = RequestMethod.POST)
    public String save(@ModelAttribute("etablissement") EtablissementModel etablissement, BindingResult results, HttpSession session){
        
        if(etablissementService.saveEtablissement(etablissement)){
            alertSuccess("etablissement.save.ok", "L'établissement a été enregistré", session);
            
        }else{
            alertError("etablissement.save.nok",  "L'établissement n'a pas été enregistré", session);
        }
        
        return "redirect:etablissements";
    }
    
    @RequestMapping("/admin/etablissement.delete")
    public String delete(@RequestParam("etablissementid") long etabId, HttpSession session){
        
        EtablissementModel etablissement = etablissementService.getEtablissementById(etabId);
        
        if(etablissementService.deleteEtablissement(etablissement)){
            alertSuccess("etablissement.save.ok", "L'établissement a été supprimé", session);
        }else{
            alertSuccess("etablissement.save.nok", "L'établissement n'a pas été supprimé", session);
        }
        
        return "redirect:etablissements";
    }
}
